//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

///import the NYT data for a given state 
use "data/nyt_states", clear


** find the last reading in the 60 hour period and the first reading after the 60 hour period
//gen ex_total60 = total + ((60-hoursfromclose)/(hoursfromclose[_n+1] - hoursfromclose)) * (total[_n+1] - total) if n60 ~= . & n60[_n+1] == .


twoway (connected total hoursfromclose, mcolor(sand) msize(vsmall) msymbol(circle) lcolor(sand) lpattern(solid) connect(stairstep)) ///
       (pcspike total hoursfromclose ex_total60 sixty if n60 ~=. & n60[_n+1] == ., lcolor(grey) lpattern(dash) ) ///
       if hoursfromclose >= 0 & hoursfromclose <= 60 & state == "georgia" ///
	   , ///
	   ytitle(Votes) ylabel(, format(%16.0gc) angle(0)) ///
	   yscale(range(0 5000000)) ///
	   xtitle(Hours from Close) xlabel(0(4)60) xmtick(0(2)60, grid) ///
	   yline(4992420, lcolor(grey) lwidth(vvthin) lpattern(dash)) ///
	   title("Total votes reported within 60 hours of polls closing") ///
	   subtitle("Georgia") ///
	   legend(off)
graph export "figures\fig1a.png", replace width(2500)


capture drop if hoursfromclose < 0
*gen dempct = 100*dem/(dem+rep)
gen lastpct = dempct if state ~= state[_n+1]
egen minpct = min(dempct),by(state)
egen maxpct = max(dempct),by(state)

capture gen twentyfour = 24
capture gen zero = 0
capture bysort state: gen n24 = _n if hoursfromclose < 24


** find the last reading in the 24 hour period and the first reading after the 24 hour period
gen ex_pct = dempct + ((24-hoursfromclose)/(hoursfromclose[_n+1] - hoursfromclose)) * (dempct[_n+1] - dempct) if n24 ~= . & n24[_n+1] == .


capture gen sixty = 60
capture bysort state: gen n60 = _n if hoursfromclose < 60

** find the last reading in the 60 hour period and the first reading after the 60 hour period
gen ex_pct60 = dempct + ((60-hoursfromclose)/(hoursfromclose[_n+1] - hoursfromclose)) * (dempct[_n+1] - dempct) if n60 ~= . & n60[_n+1] == .

twoway (pcspike lastpct zero lastpct sixty, lcolor(sand) lwidth(thin) lpattern(solid) ) ///
       (pcspike dempct hoursfromclose ex_pct60 sixty if n60 ~=. & n60[_n+1] == ., lcolor(sand) lpattern(dash) ) ///
       (connected dempct hoursfromclose if hoursfromclose >= 0 & hoursfromclose <= 60  , mcolor(sand) msize(vsmall) msymbol(circle) lcolor(sand) lpattern(solid) connect(stairstep)) ///
       if state == "georgia" ///
	   , ///
	   ylabel(, format(%16.0gc) angle(0)) ///
	   ylabel(40 45 50 55 "55%") ymtick(40(1)55, grid) ///
	   yscale(range(40 55)) ///
	   xtitle(Hours from Close) xlabel(0(4)60) xmtick(0(2)60, grid) ///
	   title("Two-party vote share for Biden" "reported within 60 hours of polls closing") ///
	   subtitle(Georgia) ///
	   legend(off)
graph export "figures/fig1b.png", replace width(2500)

